﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SSPDemo;

namespace SSP
{
    class SSPEnemyManager : SSPNode
    {
        public ArrayList enemyArray;
        public Vector2 cameraPosition { get; set; }
        Vector2 screenOriginInStageSpace;
        Vector2 screenOriginInBackgroundSpace;
        int screenWidth;
        int screenHeight;
        SSPLevelDefinition data;
        int stageBuffer = 200;
        public float heroXPosition;

        public SSPEnemyManager()
        {
        }

        public SSPEnemyManager(SSPLevelDefinition def)
        {
            screenWidth = (int)SSPDirector.Instance.windowSize().X;
            screenHeight = (int)SSPDirector.Instance.windowSize().Y;

            screenOriginInStageSpace = new Vector2(screenWidth / 2, screenHeight / 2);
            screenOriginInBackgroundSpace = cameraPosition;

            enemyArray = new ArrayList();

            data = def;
        }

        public override void update()
        {
            screenOriginInBackgroundSpace = cameraPosition;
            this.searchAndAddEnemies();
            this.updateEnemies();
        }

        public override void draw()
        {
            this.drawEnemies();
        }

        void updateEnemies()
        {
            ArrayList temp = new ArrayList();
            foreach (SSPCharacter obj in enemyArray)
            {
                obj.update();

                if (obj.isAlive)
                {
                    if (obj.type.Equals("soldier"))
                    {
                        if (obj.position.X - heroXPosition > obj.range)
                        {
                            obj.MoveLeft();
                            obj.position.X -= 5;
                        }
                        else
                        {
                            obj.Attack();
                            obj.isAttacking = true;
                        }
                    }
                    else if (obj.type.Equals("chariot"))
                    {
                    }

                    
                }
                else
                {
                    obj.buffer++;

                    if (obj.buffer >= 50)
                    {
                        temp.Add(obj);
                    }

                }

                if (!this.isPointOnStageSpace(obj.position))
                {
                    temp.Add(obj);
                }
            }

            foreach (SSPCharacter obj in temp)
            {
                if (enemyArray.Contains(obj))
                {
                    enemyArray.Remove(obj);
                }

            }
            temp.Clear();

        }

        void drawEnemies()
        {
            foreach (SSPCharacter obj in enemyArray)
            {
                obj.draw();
            }
        }

        void spawnEnemy(SSPSpawnElement element)
        {
            if(element.tagName.Equals("soldier") )
            {
                Soldier temp = new Soldier();
                temp.position = this.ConvertBackgroundSpaceToStageSpace(element.spawnPosition);
                temp.isAlive = true;
                enemyArray.Add(temp);
            }
            else if (element.tagName.Equals("chariot") )
            {
            }

            
        }

        void searchAndAddEnemies()
        {
            ArrayList temp = new ArrayList();
            foreach (SSPSpawnElement obj in data.spawnElements)
            {
               if( this.isPointOnStage(obj.spawnPosition) )
               {
                   //Console.WriteLine("EM Found!");
                   this.spawnEnemy(obj);
                   temp.Add(obj);
               }
            }

            foreach (SSPSpawnElement obj in temp)
            {
                if(data.spawnElements.Contains(obj))
                {
                    data.spawnElements.Remove(obj);
                }
            }
        }

        bool isPointOnStage(Vector2 point)
        {
            Vector2 obj = this.ConvertBackgroundSpaceToStageSpace(point);

            if (obj.X > (0 - stageBuffer) && obj.X < (screenWidth + stageBuffer))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        bool isPointOnStageSpace(Vector2 point)
        {

            if (point.X > (0 - stageBuffer) && point.X < (screenWidth + stageBuffer))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        

        public Vector2 ConvertBackgroundSpaceToStageSpace(Vector2 bgSpace)
        {
            return Vector2.Add(Vector2.Subtract(bgSpace, screenOriginInBackgroundSpace), screenOriginInStageSpace);
        }

        public Vector2 ConvertStageSpaceToBackgroundSpace(Vector2 stageSpace)
        {
            return Vector2.Add(Vector2.Subtract(stageSpace, screenOriginInStageSpace), screenOriginInBackgroundSpace);
        }

        public int ConvertBackgroundSpaceToStageSpace(int bgSpace)
        {
            return (int)((bgSpace - screenOriginInBackgroundSpace.X) + screenOriginInStageSpace.X);
        }

        public int ConvertStageSpaceToBackgroundSpace(int stageSpace)
        {
            return (int)((stageSpace - screenOriginInStageSpace.X) + screenOriginInBackgroundSpace.X);
        }

    }
}
